<?php
namespace app\interfaces\controller;

use app\interfaces\model\Ad as AdModel;
use app\interfaces\model\GoodsType as GoodsTypeModel;
use app\interfaces\model\ShopGoods as ShopGoodsModel;
use think\Request;
use think\Exception;
use think\Cache;

class HotMarket
{

    /*
     * 用户端 - 热门市场分类列表
     */
    public function market_type()
    {

        $result = AdModel::where(['parenTid' => '6'])->field('linkUrl,picUrl,skip_type')->limit(2)->select();

        $type_list = GoodsTypeModel::where(['parenTid' => '0', 'hot_recommend' => '1', 'status' => '1'])
            ->field('id,title,picUrl')
            ->order(['sort' => 'desc'])
            ->select();

        return ['code' => 200, 'message' => '成功', 'data' => $result, 'type' => $type_list];

    }

    /*
     * 用户端 - 热门市场列表
     */
    public function market_list()
    {

        $data = Request::instance()->param();

        if (empty($data['id'])) throw new Exception('not Empty');

        // banner列表
        $banner_list = AdModel::where(['parenTid' => '19'])->field('linkUrl,picUrl,skip_type')->select();


        // 商品分类
        $result = GoodsTypeModel::where(['parentStr' => ['like', '%,' . $data['id'] . ',%']])
            ->field('id,title,parenTid')
            ->select();

        // 搜索条件
        $condition['state'] = ['eq', 1];
        $condition['shop_recommend'] = ['eq', 1];

        // 是否开启审核
        if (Cache::get('web_commodity_audit') == 'Y') $condition['status'] = ['eq', '1'];

        // 条件
        if (!empty($data['id'])) $condition['goods_str'] = ['like', '%,' . $data['id'] . ',%'];


        // 商品列表
        $goods_list = ShopGoodsModel::where($condition)
            ->field('id,title,price,thumb_img,rebate_percentage,sale_number')
            ->order(['sort' => 'desc'])
            ->paginate(10, false, ['query' => $data]);

        return [
            'code'        => 200,
            'message'     => '成功',
            'banner_list' => $banner_list,
            'data'        => recursion_node($result, $data['id']),
            'goods_list'  => $goods_list
        ];

    }

}
